package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangyueshang.enums.DelFlag;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.ShopMapper;
import com.shangyueshang.model.Shop;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.request.ShopDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.ShopService;
import com.shangyueshang.utils.MyPageHelper;
import com.shangyueshang.utils.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/14 10:48
 * @version: v1.0.0
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public HttpResult save(ShopDTO shopDTO) {
		Shop shop = new Shop();
		Shop _shop = super.getById(shopDTO.getId());
		if (_shop == null) {
			shop.setCreateTime(LocalDateTime.now());
			shop.setCreator(SecurityUtil.getUsername());
		}
		if ((_shop = this.findByName(shopDTO.getName())) != null
				&& !shopDTO.getId().equals(_shop.getId())) {
			return HttpResult.fail("该店铺名已存在");
		}
		BeanUtils.copyProperties(shopDTO, shop);
		shop.setLastUpdateTime(LocalDateTime.now());
		shop.setLastUpdator(SecurityUtil.getUsername());
		return HttpResult.ok(super.saveOrUpdate(shop));
	}
	
	@Override
	public PageResult findPage(PageReq pageReq) {
		QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(SysConstants.del_flag, DelFlag.NO.val);
		queryWrapper.orderByDesc(SysConstants.create_time);
		pageReq.getLineParams().forEach(
				(k, v) -> {
					if (k.equals(SysConstants.name)) {
						queryWrapper.like(k, v);
					} else {
						queryWrapper.eq(k, v);
					}
				}
		);
		PageInfo<Shop> pageInfo =
				PageHelper.startPage(
						pageReq.getPageNum(),
						pageReq.getPageSize()
				).doSelectPageInfo(
						() -> super.list(queryWrapper)
				);
		return MyPageHelper.getPageResult(pageInfo);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public HttpResult remove(List<Long> ids) {
		ids.forEach(id -> {
			Shop shop = new Shop();
			shop.setDelFlag(DelFlag.YES.val);
			shop.setLastUpdator(SecurityUtil.getUsername());
			super.updateById(shop);
		});
		return HttpResult.ok();
	}
	
	private Shop findByName(String name) {
		QueryWrapper<Shop> queryWrapper =
				new QueryWrapper<Shop>()
						.eq(SysConstants.name, name)
						.eq(SysConstants.del_flag, DelFlag.NO.val)
						.last("limit 1");
		return super.getOne(queryWrapper);
	}
}
